import registnet.view.RegistNetPropertyBundle;

registnet.dao.UnitManagerImpl umi = new registnet.dao.UnitManagerImpl(); 
registnet.to.Unit unit;

List data;

RegistNetPropertyBundle bundle = RegistNetPropertyBundle.getInstance("/registnet/resources/registnet");
String _add = bundle.getProperty("registnet.add");
String _edit = bundle.getProperty("registnet.edit");
String _delete = bundle.getProperty("registnet.delete");
String _search = bundle.getProperty("registnet.search");
String _import = bundle.getProperty("registnet.import");
String _college = bundle.getProperty("registnet.college");
String _successful = bundle.getProperty("registnet.successful");
String _failed = bundle.getProperty("registnet.failed");
String _askconfirmation = bundle.getProperty("registnet.askconfirmation");
String _checkinfosupplied = bundle.getProperty("registnet.checkinfosupplied");



public class ItemRenderer implements ListitemRenderer {
	public void render(Listitem item, java.lang.Object data) {
		new Listcell(data).setParent(item);
		
	}
}


void retrieveItems(){											  
	data = umi.getUnitsByUnitType(registnet.controller.Constants.COLLEGE);
	String[] listModel = new String[data.size()];
	for (int i=0; i<data.size(); i++)
		listModel[i] = data.get(i);
	ListModel strset = new ListModelList(listModel);
	college_list.setModel(strset);
	college_list.setItemRenderer(new ItemRenderer());
}	


private class SelectEventListener implements EventListener {
	
	public SelectEventListener (){
	}
			
	public void onEvent(Event event) throws UiException {
		showCollegeInfo();
	}
}	


void showCollegeInfo(){
	if (contents.getFirstChild() != null){
		contents.removeChild(contents.getFirstChild());
	}
	try{
		if (college_list.getSelectedCount() == 1){
			unit = umi.retrieveUnit(college_list.getSelectedItem().getLabel());
			title.setValue(unit.getUnitName());
		}
		else if (college_list.getSelectedCount() > 1)
			title.setValue("More than one " + _college + " selected");
		else
			title.setValue("");
	}catch (Exception e) { title.setValue(""); }
}





void showForm(String action){
	try{
		contents.removeChild(contents.getFirstChild());
	} catch (Exception e) {}

	
	if (action.equals(_edit)){
		if(college_list.getSelectedCount() == 1){
			Component edit_college_form = Executions.createComponents("/WEB-INF/macros/manager_college_form_college.zul", null, null);
			title.setValue(_edit + " " + unit.getUnitCode());
			edit_college_form.setParent(contents);
			edit_college_form.getFellow("college_code").setValue(unit.getUnitCode());
			edit_college_form.getFellow("college_name").setValue(unit.getUnitName());
			edit_college_form.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener(action));
		}
		else{
			alert("Select one " + _college);
		}
	}
	
	else if (action.equals(_add)){
		Component edit_college_form = Executions.createComponents("/WEB-INF/macros/manager_college_form_college.zul", null, null);
		title.setValue(_add + " " + _college);
		edit_college_form.setParent(contents);
		edit_college_form.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener(action));
	}
	
	
}


void deleteCollege(){
	if(college_list.getSelectedCount() != 0){
		int isConfirmed = Messagebox.show(_askconfirmation, _delete + " " + _college, Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
		if (isConfirmed == Messagebox.YES){
			boolean isError = false;
			int errorCount = 0;
			Iterator i = college_list.getSelectedItems().iterator();
			while(i.hasNext()){
				try{
					String temp = i.next().getLabel();
					umi.deleteUnit(temp);	
				}
				catch (Exception e) {
					isError = true;
					errorCount++;
				}
			}
			
			if (isError){ alert(_delete + " " + _failed + ": " + errorCount + " " + _colleges); }
			
			else{
				title.setValue(_delete + " " + _college + " " + _successful);
				if (contents.getFirstChild() != null){
					contents.removeChild(contents.getFirstChild());
				}
				data = umi.getUnitsByUnitType(registnet.controller.Constants.COLLEGE);
				retrieveItems();
			}
		}			
	}
}

void importCollege(){
	org.zkoss.util.media.Media media = Fileupload.get();
	if(media != null)
		umi.importColleges(media.getStringData());
	retrieveItems();
}


void createNewCollege(){
	if (isValid()){
		try{
			umi.createNewUnit(college_code.value, college_name.value, registnet.controller.Constants.COLLEGE);
			retrieveItems();
			data = umi.getUnitsByUnitType(registnet.controller.Constants.COLLEGE);
			title.setValue(_add + " " + _college + " " + _successful);
			try{
				contents.removeChild(contents.getFirstChild());
			} catch (Exception e) { }
		} catch(Exception e) { alert(_checkinfosupplied); }
	}
	else{
		alert(_checkinfosupplied);
	}
}


void editCollege(){
	if (isValid()){
		try{
			umi.updateUnit(title.value, college_code.value, college_name.value, registnet.controller.Constants.COLLEGE);
			retrieveItems();
			data = umi.getUnitsByUnitType(registnet.controller.Constants.COLLEGE);
			title.setValue(_edit + " " + _college + " " + _successful);
			try{
				contents.removeChild(contents.getFirstChild());
			} catch (Exception e) { }
		} catch (Exception e) { alert(_checkinfosupplied); }
	}
	else{
		alert(_checkinfosupplied);
	}
}


boolean isValid(){
	if (college_code.value.isEmpty())
		return false;
	if (college_code.value.startsWith(" "))
		return false;
	if (college_name.value.isEmpty())
		return false;
	if (college_name.value.startsWith(" "))
		return false;
	return true;
}


private class SubmitEventListener implements EventListener {
	String submitType;
	public SubmitEventListener (String submitType){
		this.submitType = submitType; 
	}
			
	public void onEvent(Event event) throws UiException {
		if(this.submitType.equals(_add))
			createNewCollege();
		else if (this.submitType.equals(_edit))
			editCollege();
	}
}


void filter(String string){

	ListIterator i = data.listIterator();
	List list = new java.util.Vector();
	while(i.hasNext()){
		String temp = i.next(); 
		if(temp.toUpperCase().startsWith(string.toUpperCase())){
			list.add(temp);
		}
	}
	college_list.getModel().clear();
	college_list.getModel().addAll(list);
}
